package zheezes.protocol.impl.gbw.afn0d;

import java.util.ArrayList;
import java.util.List;

import zheezes.protocol.impl.gbw.DataUnit;
import zheezes.protocol.impl.gbw.format.DataForm11;
import zheezes.protocol.impl.gbw.format.DataForm14;
import zheezes.protocol.impl.gbw.format.DataForm15;
import zheezes.protocol.impl.gbw.format.DataForm20;


public class Afn0DUpF1DataUnit extends DataUnit {
	private DataForm20 fdate = new DataForm20();
	private DataForm15 ttime = new DataForm15();
	private byte rates; // the number of rate
	private DataForm14 activeEnergy = new DataForm14();
	private List<DataForm14> activeEnergyList = new ArrayList<DataForm14>();
	private DataForm11 reactiveEnergy = new DataForm11();
	private List<DataForm11> reactiveEnergyList = new ArrayList<DataForm11>();
	private DataForm11 firstQuadReactiveEnergy = new DataForm11();
	private List<DataForm11> firstQuadReactiveEnergyList = new ArrayList<DataForm11>();
	private DataForm11 fourthQuadReactiveEnergy = new DataForm11();
	private List<DataForm11> fourthQuadReactiveEnergyList = new ArrayList<DataForm11>();

	@Override
	public int sizeOfData() {
		int size = 0;
		size += fdate.sizeof() + ttime.sizeof() + 1;
		size += activeEnergy.sizeof();
		for (DataForm14 df : activeEnergyList) {
			size += df.sizeof();
		}
		size += reactiveEnergy.sizeof();
		for (DataForm11 df : reactiveEnergyList) {
			size += df.sizeof();
		}
		size += firstQuadReactiveEnergy.sizeof();
		for (DataForm11 df : firstQuadReactiveEnergyList) {
			size += df.sizeof();
		}
		size += fourthQuadReactiveEnergy.sizeof();
		for (DataForm11 df : fourthQuadReactiveEnergyList) {
			size += df.sizeof();
		}
		return size;
	}

	@Override
	public int getData(byte[] buf, int offset) {
		int idx = offset;
		idx += fdate.getBytes(buf, idx);
		idx += ttime.getBytes(buf, idx);
		buf[idx++] = rates;
		idx += activeEnergy.getBytes(buf, idx);
		for (DataForm14 df : activeEnergyList) {
			idx += df.getBytes(buf, idx);
		}
		idx += reactiveEnergy.getBytes(buf, idx);
		for (DataForm11 df : reactiveEnergyList) {
			idx += df.getBytes(buf, idx);
		}
		idx += firstQuadReactiveEnergy.getBytes(buf, idx);
		for (DataForm11 df : firstQuadReactiveEnergyList) {
			idx += df.getBytes(buf, idx);
		}
		idx += fourthQuadReactiveEnergy.getBytes(buf, idx);
		for (DataForm11 df : fourthQuadReactiveEnergyList) {
			idx += df.getBytes(buf, idx);
		}

		return idx - offset;
	}

	@Override
	public int setData(byte[] buf, int offset, int max) {
		int idx = offset;
		idx += fdate.setBytes(buf, idx);
		idx += ttime.setBytes(buf, idx);
		rates = buf[idx++];
		idx += activeEnergy.setBytes(buf, idx);
		for (int i = 0; i < rates; i++) {
			DataForm14 df = new DataForm14();
			idx += df.setBytes(buf, idx);
			activeEnergyList.add(df);
		}
		idx += reactiveEnergy.setBytes(buf, idx);
		for (int i = 0; i < rates; i++) {
			DataForm11 df = new DataForm11();
			idx += df.setBytes(buf, idx);
			reactiveEnergyList.add(df);
		}
		idx += firstQuadReactiveEnergy.setBytes(buf, idx);
		for (int i = 0; i < rates; i++) {
			DataForm11 df = new DataForm11();
			idx += df.setBytes(buf, idx);
			firstQuadReactiveEnergyList.add(df);
		}
		idx += fourthQuadReactiveEnergy.setBytes(buf, idx);
		for (int i = 0; i < rates; i++) {
			DataForm11 df = new DataForm11();
			idx += df.setBytes(buf, idx);
			fourthQuadReactiveEnergyList.add(df);
		}

		return idx - offset;
	}

	public DataForm20 getFdate() {
		return fdate;
	}

	public void setFdate(DataForm20 fdate) {
		this.fdate = fdate;
	}

	public DataForm15 getTtime() {
		return ttime;
	}

	public void setTtime(DataForm15 ttime) {
		this.ttime = ttime;
	}

	public byte getRates() {
		return rates;
	}

	public void setRates(byte rates) {
		this.rates = rates;
	}

	public DataForm14 getActiveEnergy() {
		return activeEnergy;
	}

	public void setActiveEnergy(DataForm14 activeEnergy) {
		this.activeEnergy = activeEnergy;
	}

	public DataForm11 getReactiveEnergy() {
		return reactiveEnergy;
	}

	public void setReactiveEnergy(DataForm11 reactiveEnergy) {
		this.reactiveEnergy = reactiveEnergy;
	}

	public DataForm11 getFirstQuadReactiveEnergy() {
		return firstQuadReactiveEnergy;
	}

	public void setFirstQuadReactiveEnergy(DataForm11 firstQuadReactiveEnergy) {
		this.firstQuadReactiveEnergy = firstQuadReactiveEnergy;
	}

	public DataForm11 getFourthQuadReactiveEnergy() {
		return fourthQuadReactiveEnergy;
	}

	public void setFourthQuadReactiveEnergy(DataForm11 fourthQuadReactiveEnergy) {
		this.fourthQuadReactiveEnergy = fourthQuadReactiveEnergy;
	}
}
